package com.neato.ui.comp;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.neato.ui.comp.listener.ListViewDragListener;

public class DragableListViewActivity extends ListActivity {
	private static String[] items = { "第1项", "第2项", "第3项", "第4项", "第5项", "第6项", "第7项", "第8项","第9项","第10项" };
	private MyAdapter adapter = null;
	private ArrayList<String> array = new ArrayList<String>(
			Arrays.asList(items));

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);

		adapter = new MyAdapter();
		setListAdapter(adapter);
		final DragableListView dlv = (DragableListView) getListView();

		dlv.setOnDragListener(onDrop);

		dlv.getAdapter();
		dlv.setLongClickable(true);
/*		dlv.setOnItemLongClickListener(new OnItemLongClickListener(){

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				Log.i("onLongClick", "longclick");
				dlv.setDragEnable(true);
				return true;
			}
			
		});*/
	}

	private ListViewDragListener onDrop = new ListViewDragListener() {
		@Override
		public void drop(int from, int to) {
			String item = adapter.getItem(from);

			adapter.remove(item);
			adapter.insert(item, to);

		}
	};

	class MyAdapter extends ArrayAdapter<String> {

		MyAdapter() {
			super(DragableListViewActivity.this, R.layout.mylistview, array);
		}

		public ArrayList<String> getList() {
			return array;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.mylistview, parent, false);
			}
			TextView label = (TextView) row.findViewById(R.id.label);
			label.setText(array.get(position));
			return (row);
		}
	}
}